<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <style>
        img{
            width: 50%;
            position: absolute;
        }
    </style>
    <div id="box">
        <img src="../images/lunbo3.jpg" v-move alt="">
    </div>
    <script>
        var vm = new Vue({
            el: "#box",
            directives: {
                move: function (el){
                    //按下鼠标
                    el.onmousedown = function(e){
                        var initX = el.offsetLeft;
                        var initY = el.offsetTop;
                        var offsetX = e.clientX - initX;
                        var offsetY = e.clientY - initY;
                        //移动鼠标
                        document.onmousemove = function(e){
                            var x = e.clientX - offsetX;
                            var y = e.clientY - offsetY;
                            var maxX = document.documentElement.clientWidth - el.offsetWidth;
                            var maxY = document.documentElement.clientHeight - el.offsetHeight;
                            if(x <= 0) x=0;
                            if(y <= 0) y=0;
                            if(x >= maxX) x= maxX;
                            if(y >= maxY) y= maxY;
                            el.style.left = x + "px";
                            el.style.top = y + "px";
                            return false;
                        }
                    }
                    //松开鼠标
                    document.onmouseup = function(){
                        document.onmousemove = null;
                    }
                }
            },
        })

    </script>
    
</body>
</html>